111. 二叉树的最小深度
111. 二叉树的最小深度
Similar Question
leading to the advanced question
Solution Tips
方案一: BFS
const minDepth = (root) => {
if (root == null) return 0;
const queue = [root]; // 根节点入列
let depth = 1; // 当前层的深度
while (queue.length) { // 直到清空队列
const levelSize = queue.length; // 当前层的节点个数
for (let i = 0; i < levelSize; i++) { // 遍历 逐个出列
const cur = queue.shift(); // 出列
if (cur.left == null && cur.right == null) { // 如果没有孩子,直接返回所在层数
return depth;
}
if (cur.left) queue.push(cur.left); // 有孩子,让孩子入列
if (cur.right) queue.push(cur.right);
}
depth++; // 肯定有下一层,如果没有早就return了
}
};